package com.nmsl.statistic.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.statistic.vo.McslfAttDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nmsl.statistic.mapper.RetailAttMapper;
import com.nmsl.statistic.domain.RetailAtt;
import com.nmsl.statistic.service.IRetailAttService;

/**
 * 零售附加项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-16
 */
@Service
public class RetailAttServiceImpl implements IRetailAttService 
{
    @Autowired
    private RetailAttMapper retailAttMapper;

    /**
     * 查询零售附加项目
     * 
     * @param retailAttId 零售附加项目主键
     * @return 零售附加项目
     */
    @Override
    public RetailAtt selectRetailAttByRetailAttId(String retailAttId)
    {
        return retailAttMapper.selectRetailAttByRetailAttId(retailAttId);
    }



    @Override
    public List<RetailAtt> schaeckAttList(RetailAtt retailAtt,String name)
    {
        return retailAttMapper.selectRetailAttList(retailAtt);
    }
    /**
     * 新增零售附加项目
     * 
     * @param retailAtt 零售附加项目
     * @return 结果
     */
    @Override
    public int insertRetailAtt(RetailAtt retailAtt)
    {
        return retailAttMapper.insertRetailAtt(retailAtt);
    }

    /**
     * 修改零售附加项目
     * 
     * @param retailAtt 零售附加项目
     * @return 结果
     */
    @Override
    public int updateRetailAtt(RetailAtt retailAtt)
    {
        return retailAttMapper.updateRetailAtt(retailAtt);
    }

    /**
     * 批量删除零售附加项目
     * 
     * @param retailAttIds 需要删除的零售附加项目主键
     * @return 结果
     */
    @Override
    public int deleteRetailAttByRetailAttIds(String[] retailAttIds)
    {
        return retailAttMapper.deleteRetailAttByRetailAttIds(retailAttIds);
    }

    /**
     * 删除零售附加项目信息
     * 
     * @param retailAttId 零售附加项目主键
     * @return 结果
     */
    @Override
    public int deleteRetailAttByRetailAttId(String retailAttId)
    {
        return retailAttMapper.deleteRetailAttByRetailAttId(retailAttId);
    }

    @Override
    public List<RetailAtt> selectCheackAttList(RetailAtt retailAtt ,Long tollManageId, String itemName) {
        //        传入 项目名itemName    以及  订单ID tollManageId
        List<RetailAtt> retailAtts = retailAttMapper.selectRetailAttList(retailAtt);
        return retailAtts;
    }
    /**
     * 查询零售附加项目列表
     *
     * @param retailAtt 零售附加项目
     * @return 零售附加项目
     */
    @Override
    public List<RetailAtt> selectRetailAttList(RetailAtt retailAtt)
    {
        return retailAttMapper.selectRetailAttList(retailAtt);
    }
    @Override
    public  List<McslfAttDetails> checkMcslfAttrlist(McslfAttDetails retailAtt) {
        List<McslfAttDetails> list =  retailAttMapper.checkMcslfAttrlist(retailAtt);

//        计算属性
        List<McslfAttDetails> collect = list.stream().map(item -> {
            //        单个 成本总价
            BigDecimal itemCostPric = new BigDecimal(item.getCostPrice());
            BigDecimal itemNum = new BigDecimal(item.getAttNum());
            item.setCostPriceAmount(itemCostPric.multiply(itemNum).toString());
            // 单个成本利润
            BigDecimal bigDecimal = new BigDecimal(item.getCostPriceAmount());
            item.setFitAmout(item.getAttPrice().subtract(bigDecimal).toString());
            return item;
        }).collect(Collectors.toList());
//        总成本
        BigDecimal allCostPriceAmount = new BigDecimal(0);
//        总销售总价
        BigDecimal allAttPrice = new BigDecimal(0);
//        总利润
        BigDecimal allFitAmout = new BigDecimal(0);

        for (McslfAttDetails item : collect) {
            BigDecimal bigDecimal = new BigDecimal(item.getCostPriceAmount());
            allCostPriceAmount = allCostPriceAmount.add(bigDecimal);
            allAttPrice = allAttPrice.add(item.getAttPrice());
        }
        allFitAmout = allAttPrice.subtract(allCostPriceAmount);

        McslfAttDetails mcslfAttDetails = new McslfAttDetails();
        mcslfAttDetails.setRetailOrderId(1l);
        mcslfAttDetails.setFitAmout(allFitAmout.toString());
        mcslfAttDetails.setAttPrice(allAttPrice);
        mcslfAttDetails.setCostPriceAmount(allCostPriceAmount.toString());
        collect.add(0,mcslfAttDetails);
        return collect;
    }
}
